<?php

/**
 * Stage form.
 *
 * @package    tfe
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class StageForm extends BaseStageForm {

    private static $intervalle_jour_seance = 1;
    private static $separateur_date = '-';

    public function configure() {
        unset($this['created_at'], $this['updated_at'], $this['deleted_at']);

        $this->widgetSchema['commentaire'] =  new sfWidgetFormCKEditor(array('jsoptions'=>array('toolbar'=>'Full','skin'=>'kama', 'height'=>'300')));

        $this->widgetSchema['animateur']->setOption('renderer_class', 'sfWidgetFormDoctrineJQueryAutocompleter');
        $this->widgetSchema['animateur']->setOption('renderer_options', array(
                'model' => 'Animateur',
                'url'   => '/animateur/autocomplete',
        ));

        $this->widgetSchema['date_de_debut'] = new sfWidgetFormJQueryDate(array('culture'=>'FR'));
        $this->widgetSchema['date_de_fin'] = new sfWidgetFormJQueryDate(array('culture'=>'FR'));


        $dateWidget = new sfWidgetFormI18nDate(array(
                        'format' => '%day%/%month%/%year%',
                        'culture'=>'fr'
        ));
        $this->widgetSchema['date_de_debut'] = new sfWidgetFormJqueryDate(array(
                        'date_widget' => $dateWidget,
                        'culture'=>'fr_FR'
        ));


        $dateWidget = new sfWidgetFormI18nDate(array(
                        'format' => '%day%/%month%/%year%',
                        'culture'=>'fr'
        ));
        $this->widgetSchema['date_de_fin'] = new sfWidgetFormJqueryDate(array(
                        'date_widget' => $dateWidget,
                        'culture'=>'fr_FR'
        ));


        $this->validatorSchema->setPostValidator(
                new sfValidatorSchemaCompare('date_de_debut', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'date_de_fin',
                array(),
                array('invalid' => 'La date de début doit précéder la date de fin !')
                )
        );

    }

    public function save($con = null) {
        $stage = parent::save();

        if (!is_null($stage)) {
            // supprimer ce qui a deja ete insere dans la table EVENEMENT
            EvenementTable::supprimerSeancesStage($stage->getId(), date('Y-m-d'));

            // test validite sur le formulaire
            if ($this->formulaireValide($stage)) {
                // champs mandatory : libelle, date_de_debut, date_de_fin
                $dateDebut = $stage->getDateDeDebut();
                $dateFin   = $stage->getDateDeFin();

                // format timestamp pour les comparaisons
                $dateSeance    = strtotime($dateDebut);
                $dateSeanceFin = strtotime($dateFin);

                // tant que la date de seance n'est superieur a la date de fin
                while ($dateSeance <= $dateSeanceFin) {
                    // format de la date de la seance
                    $dateSeance = date('Y-m-d',$dateSeance);

                    // placer les differents attributs
                    $seanceStage = new SeanceStage();
                    $seanceStage->setIdStage($stage->getId());
                    $seanceStage->setLibelle($stage->getLibelle());
                    $seanceStage->setDate($dateSeance);
                    $seanceStage->setHeureDeDebut($stage->getHeureDebut());
                    $seanceStage->setHeureDeFin($stage->getHeureFin());
                    $seanceStage->setLieu($stage->getLieu());
                    $seanceStage->setCommentaire($stage->getCommentaire());
                    $seanceStage->setType('stage');

                    // insertion de la seance
                    $seanceStage->save();

                    // ajout de l'intervalle a la date de la seance courante
                    list($year, $month, $day) = explode('-', $dateSeance);
                    $timestamp  = mktime(0,0,0, $month, $day + self::$intervalle_jour_seance, $year);
                    $dateSeance = date('Y-m-d',$timestamp);
                    $dateSeance = strtotime($dateSeance);
                }
            } else {
                throw new Exception('Formulaire non valide');
            }
        }
        return $stage;
    }

    private function formulaireValide($stage) {
        $valid = false;
        // test des elements passes dans le form de seance de stage
        if ($stage != null) {
            if (!is_null($stage->getId()) && is_numeric($stage->getId())) {
                if (!is_null($stage->getLibelle())) {
                    if (!is_null($stage->getDateDeDebut()) && !is_null($stage->getDateDeFin())) {
                        if ($this->dateValide($stage->getDateDeDebut(), $stage->getDateDeFin(), $stage->getHeureDebut(), $stage->getHeureFin())) {
                            if (!is_null($stage->getLieu())) {
                                $valid = true;
                            }
                        }
                    }
                }
            }
        }
        return $valid;
    }

    private function dateValide($dateDebut, $dateFin,$heureDebut, $heureFin) {
        $valid = false;

        // test des dates
        $dateDeb = new DateTime($dateDebut);
        $dateFin = new DateTime($dateFin);

        if ($dateDeb < $dateFin) {
            $valid = true;
        } else if ($dateDeb > $dateFin) {
            $valid = false;
        } else if ($dateDeb == $dateFin) {
            // test sur les heures [pas needed mais on sait jms]
            if (!is_null($heureDebut) && !is_null($heureFin)) {
                if (!empty($heureDebut) && !empty($heureFin)) {
                    $dateDeb = getdate(strtotime($heureDebut));
                    $dateFin = getdate(strtotime($heureFin));

                    if ($dateFin['hours'] > $dateDeb['hours']) {
                        $valid = true;
                    } else if ($dateFin['hours'] < $dateDeb['hours']) {
                        $valid = false;
                    } else if ($dateFin['hours'] == $dateDeb['hours']) {
                        if ($dateFin['minutes'] > $dateDeb['minutes']) {
                            $valid = true;
                        } else {
                            $valid = false;
                        }
                    }
                }
            }
        }
        return $valid;
    }
}








